/**
 * 用户信息编辑
 */

require(['jquery','../modules/pageSize','../modules/popup', 'ajaxupload'], function($, pageSetSize, popup){
    //设置html根目录字体大小
    setRootFontSize(pageSetSize);
    var vMsgObj = $(".validate-tips").children('p').eq(0);
    var _nickname, _sex, _birthday, _msg;
    var isEdit = false, isAjaxSend = false;
    function enableEdit(o){
        $(o).hide().siblings('a').show();
        if($(o).hasClass('edit')){
            $('.person-info').addClass('edit');
            $('.person-info input').attr('disabled', false);
            isEdit = true;
        }else{
            $('.person-info').removeClass('edit');
            $('.person-info input').attr('disabled', true);
            isEdit = false;
        }
    }
    // 编辑资料
    $('.person-info h1 a').on('click',function(){
        var _this = $(this);
        if(!isEdit){
            enableEdit(_this);
        }else{
            _nickname = $("#nickname").val();
            _sex = $("#sex").val();
            _birthday = $("#birthday").val();
            _msg = mbValidator.isNickname(_nickname);
            if(_nickname !="" && _msg != ""){
                vMsgObj.text('昵称'+_msg);
                popup.popup(".validate-tips");
                return;
            }
            if(_sex != ""  && _sex != "男" && _sex != "女"){
                vMsgObj.text('性别请输入男或女');
                popup.popup(".validate-tips");
                return;
            }
            if(_birthday != ""){
                var bdYear = parseInt(_birthday.substr(0, 4));
                var now = new Date();
                if(now.getFullYear() - bdYear <= 6){
                    vMsgObj.text('不好意思！根据您的生日判断，您的年龄小于6岁!');
                    popup.popup(".validate-tips");
                    return;
                }
            }
			if(isAjaxSend) return;
            isAjaxSend = true;
            popup.loadPopup();
            $.ajax({
                url: '/Home/Member/saveMemberInfo'+getFixedExtraUrl(),
                type: 'POST',
                dataType: 'json',
                data: {nickname: _nickname, sex: _sex, birthday: _birthday},
                success: function(rs){
                    enableEdit(_this);
                    popup.removeLoad();
                    if(rs.stat){
                        //不作处理
                    }
					popup.alertPopup(rs.msg);
					isAjaxSend = false;
                },
                error: function(){
                    popup.removeLoad();
                    popup.alertPopup('服务器异常，请稍后重试！');
					isAjaxSend = false;
                }
            });
        }
    });

    $('.person-info li').each(function(){
        $(this).find('.clear').on('click',function(e){
            e.stopPropagation();
            $(this).siblings('input').val('');
            $(this).hide();
        })
        $(this).find('input').focus(function(e){
            e.stopPropagation();
            $(this).siblings('.clear').show();
        })
    });
    // 绑定手机
    $('#mem-bind-phone').click(function(e){
        _global.checkPhoner.runOnlyPhone(e, "");
    });
    //地址管理
    $('.member-address .choice').on('click',function(){
        $(this).toggleClass('active').parents('li').siblings().find('.choice').removeClass('active');
    });
    $('.member-address .delete').on('click',function(){
        $(this).parents('li').remove();
    });
    $("#upImgBtn").AjaxFileUpload({
        action : "/Home/Member/post_member_logo",
        type: "img",
        callback: function(key){
            if(key == "type"){
                _global.promote.alertPopup("请选择图片类型的文件！");
            }else if(key == "process"){
                _global.promote.alertPopup("图片正在上传,请稍等！");
            }
        },
        onChange: function(){
            popup.loadPopup();
        },
        isOutOfRange: function(){
            return false;
        },
        onComplete: function(filename, response) {
            var upfname = response.file;
            if(upfname == ""){
                popup.removeLoad();
                _global.promote.alertPopup("图片上传失败！");
            }else{
                $("#user-logo-pic").attr("src", upfname);
                $("#usericon").val(upfname);
                $.ajax({
                    url: '/Home/Member/saveMemberInfo'+getFixedExtraUrl(),
                    type: 'POST',
                    dataType: 'json',
                    data: {act: "change_icon", usericon: $("#usericon").val()},
                    success: function(rs){
                        popup.removeLoad();
                        if(rs.stat){
                            popup.alertPopup(rs.msg);
                        }else{
                            popup.alertPopup(rs.msg);
                        }
                    },
                    error: function(){
                        popup.removeLoad();
                        popup.alertPopup('服务器异常，请稍后重试！');
                    }
                });
            }
        }
    });
});